package logic

import (
	"context"
	"fmt"
	"he-ma/models"

	"he-ma/HeMarpc/heMarpc"
	"he-ma/HeMarpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加
func (l *ShopAddLogic) ShopAdd(in *heMarpc.ShopAddRequest) (*heMarpc.ShopAddResponse, error) {
	// todo: add your logic here and delete this line

	var shop models.Shop
	models.DB.Debug().Where("title = ?", in.Title).Find(&shop)
	if shop.ID != 0 {
		return nil, fmt.Errorf("当前商品已存在")
	}

	shop = models.Shop{
		Title:   in.Title,
		Price:   float64(in.Price),
		Profile: in.Profile,
	}

	models.DB.Debug().Create(&shop)

	return &heMarpc.ShopAddResponse{
		ShopId: int64(shop.ID),
	}, nil
}
